/*
 * @Description: 
 * @Author: 赵大壮
 * @Date: 2021-12-07 17:23:35
 * @LastEditTime: 2022-05-23 17:34:30
 * @LastEditors: 赵大壮
 */

class DZModelUser {
    token = "";
    role = null;
    roleInfo = "";
    login_time = 0;



    constructor() {
        var userData = window.localStorage.getItem("nyUser");
        if (userData) {
            this.parse(JSON.parse(userData));
        }
    }

    parse(data: any) {
        this.token = data.token ?? "";
        this.role = data.role;
        this.roleInfo = data.roleInfo;
        this.login_time = data.login_time ?? 0;
    }



    isLogin() {
        return !(this.token.nyIsEmpty());
    }

    saveUserData(data: any) {
        data["login_time"] = new Date().getTime();
        window.localStorage.setItem("nyUser", JSON.stringify(data));
    }

    /**
     * @description: 从缓存更新用户数据，常用于浏览器新增标签后退出
     */
    updateCache() {
        var userData = window.localStorage.getItem("nyUser");
        if (userData) {
            this.parse(JSON.parse(userData));
        }
    }

    /**
     * 校验是否登录过期，过期时间2个小时
     * @returns 
     */
    checkOverdue() {
        if (this.login_time == 0) {
            return false;
        }
        var overdue = false;
        var now = new Date().getTime();
        var old = this.login_time;
        if ((now - old) > (12 * 60 * 60 * 1000)) {
            overdue = true;
        }
        return overdue;
    }

    logout() {
        window.localStorage.removeItem("nyUser");
    }

}

export default DZModelUser